package com.mankind.ssm.service;

import com.mankind.ssm.common.CONSTANT;
import com.mankind.ssm.common.Page;
import com.mankind.ssm.mapper.RoleMapper;
import com.mankind.ssm.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Mankind on 2017/7/15.
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService{
    @Autowired
    private RoleMapper roleMapper;
    public List<Role> selectAllRole(){
        return roleMapper.selectAll();
    }

    public Integer getTotalNumber() {
        Integer num=roleMapper.getTotalNumber();
        if(num==0) return 0;
        return (num-1)/ CONSTANT.USERINFO_LIST_SIZE+1;
    }

    public Integer getTotalNumber(String text) {
        int num=roleMapper.getFuzzyQueryTotalNumber(text);
        System.out.println("num:"+num);
        if(num==0) return 0;
        return (num-1)/ CONSTANT.USERINFO_LIST_SIZE+1;
    }

    public Page<Role> getRoleByPage(int pageSize, int currentPage) {
        Page<Role> p=new Page<Role>();
        int start=(currentPage-1)*pageSize;
        List<Role> u=roleMapper.findByPage(start,pageSize);
        p.setNumberPage(pageSize);
        p.setObj(u);
        return p;
    }

    public Page<Role> fuzzyQueryByPage(int currentPage, int pageSize, String text) {
        Page<Role> p=new Page<Role>();
        int start=(currentPage-1)*pageSize;
        List<Role> u=roleMapper.fuzzyQueryByPage(start,pageSize,text);
        p.setNumberPage(pageSize);
        p.setObj(u);
        return p;
    }

    public Role getRoleById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    public void insertRole(Role role) {
        roleMapper.insert(role);
    }

    public void deleteByRoleId(Integer roleId) {
        roleMapper.deleteByPrimaryKey(roleId);
    }

    public void updateRole(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }
}
